'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 联盟任务随机事件
 * @author sundbcn@gmail.com (sun debing)
 */

var dataApi = require('../libs/dataApi');
var _ = require('lodash');

/**
 * 联盟任务随机事件
 * @param  {[type]} vip [description]
 * @return {[type]}     [description]
 */
exports.randomReward = function () {
    var total_probability = 0;
    var list = [];
    var random_list = [];

    _.each(_getConf(), function (item) {
        var union_item = _.clone(item);

        total_probability += union_item.probability;
        union_item.total_probability = total_probability;
        list.push(union_item);
    });

    var ii = 0;
    while (random_list.length < 3) {
        ii++;

        var random_item = _randomItem(list, total_probability);

        if (_.indexOf(random_list, random_item) == -1) {
            random_list.push(random_item);
        }
        if (ii >= 100000) {
            console.log('fuck you lucky is 0');
            break;
        }
    }
    return random_list;
};
var _randomItem = function (list, total_probability) {
    var item;
    var random = _.random(0, total_probability - 1);
    _.some(list, function (pro_item) {
        if (pro_item.total_probability >= random) {
            item = pro_item;
            return true;
        }
    });
    return item;
};
/**
 * 联盟item
 * @param  {[type]} vip [description]
 * @return {[type]}     [description]
 */
exports.getUnionMissionItem = function (num) {
    return dataApi.union_mission.findById(num) || {};
};

/**
 * 联盟刷新消耗表
 * @param  {[type]} vip [description]
 * @return {[type]}     [description]
 */
exports.refreshDiamond = function (num) {
    return dataApi.union_mission_refresh.findById(num) || [];
};

/**
 * 联盟宝箱conf
 * @param  {[type]} vip [description]
 * @return {[type]}     [description]
 */
exports.unionBox = function (star) {
    return dataApi.union_star_box.findById(star) || [];
};

var _getConf = function () {
    var list = _.clone(dataApi.union_mission.all());
    if (!list) {
        console.error('union  mission not find -----------');
    }
    return list || [];
};
